package justbuy.item.model;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.type.TypeReference;
import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableComponent;
import org.compass.annotations.SearchableProperty;
import org.ironrhino.core.metadata.NotInCopy;
import org.ironrhino.core.metadata.NotInJson;
import org.ironrhino.core.model.BaseEntity;
import org.ironrhino.core.model.Ordered;
import org.ironrhino.core.util.JsonUtils;

/*
 * 商品
 */
@Searchable(alias = "item")
public class Item extends BaseEntity implements Ordered {

	private static final long serialVersionUID = 1876365527076787416L;

	@SearchableProperty(boost = 3)
	private String name;

	private List<String> pictures;

	private BigDecimal price;

	private int stock;

	private String sku;

	private String barcode;

	private int displayOrder;

	@NotInCopy
	@NotInJson
	@SearchableComponent
	private Category category;

	private List<Property> properties;

	private Date createDate = new Date();

	private Date modifyDate;

	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public String getSku() {
		return sku;
	}

	public void setSku(String sku) {
		this.sku = sku;
	}

	public String getBarcode() {
		return barcode;
	}

	public void setBarcode(String barcode) {
		this.barcode = barcode;
	}

	public int getDisplayOrder() {
		return displayOrder;
	}

	public void setDisplayOrder(int displayOrder) {
		this.displayOrder = displayOrder;
	}

	public int getStock() {
		return stock;
	}

	public void setStock(int stock) {
		this.stock = stock;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Date getModifyDate() {
		return modifyDate;
	}

	public void setModifyDate(Date modifyDate) {
		this.modifyDate = modifyDate;
	}

	public List<String> getPictures() {
		return pictures;
	}

	public void setPictures(List<String> pictures) {
		this.pictures = pictures;
	}

	public String getPicturesAsString() {
		if (pictures == null || pictures.isEmpty())
			return null;
		return JsonUtils.toJson(pictures);
	}

	public void setPicturesAsString(String str) {
		if (StringUtils.isNotBlank(str))
			try {
				pictures = JsonUtils.fromJson(str,
						new TypeReference<List<String>>() {
						});
			} catch (Exception e) {
				e.printStackTrace();
			}
	}

	public List<Property> getProperties() {
		return properties;
	}

	public void setProperties(List<Property> properties) {
		this.properties = properties;
	}

	public String getPropertiesAsString() {
		if (properties == null || properties.isEmpty())
			return null;
		return JsonUtils.toJson(properties);
	}

	public void setPropertiesAsString(String str) {
		if (StringUtils.isNotBlank(str))
			try {
				properties = JsonUtils.fromJson(str,
						new TypeReference<List<Property>>() {
						});
			} catch (Exception e) {
				e.printStackTrace();
			}
	}

	public int compareTo(Object object) {
		if (!(object instanceof Ordered))
			return 0;
		Ordered ordered = (Ordered) object;
		if (this.getDisplayOrder() != ordered.getDisplayOrder())
			return this.getDisplayOrder() - ordered.getDisplayOrder();
		return this.toString().compareTo(ordered.toString());
	}

	@Override
	public String toString() {
		return getName();
	}
}
